<template>
  <DetailsFormBox :title="dataInfo?.table?.title" :type="route.query.type" :form="data.form" :btns="[
    {
      title: '取消',
      icon: '',
      type: '',
      isNotRule: true,
      click: (form: any) => {
        close()
      },
    },
    {
      title: '保存',
      icon: '',
      type: 'primary',
      click: (form: any) => {
        saveBtn(form)
      },
    },
  ]" :loading="data.loading" :json="dataInfo">
    <template #default="{ form }">
      <div class="formMain">
        <ElFormItemBox label="客户名称" prop="customerCode" isRules>
          <ElSelectBox :data="form" dataName="customerCode" :options="options.customer" @changeBtn="(value: string) => {
            proxy.$api.OrderBusniess.GetMatchProductViewList({
              queryBy: 0,
              queryValue: value,
            })
              .then((res: any) => {
                options.productView = res.map((el: any) => ({
                  label: el.productName,
                  value: el.productCode,
                }));
              })
              .catch(() => { });
          }" />
        </ElFormItemBox>
        <ElFormItemBox label="产品来源" :prop="`productSource`" isRules>
          <ElRadioBox :data="form" dataName="productSource" :options="proxy.$optionsArr.productSource" />
        </ElFormItemBox>
        <ElFormItemBox label="产品类型" :prop="`productType`" isRules>
          <ElRadioBox :data="form" dataName="productType" :options="options.productType" />
        </ElFormItemBox>
        <ElFormItemBox label="产品版面" :prop="`productLayout`" isRules>
          <ElRadioBox :data="form" dataName="productLayout" :options="[
            {
              label: '新版',
              value: '新版'
            },
            {
              label: '改版',
              value: '改版',
              disabled: options.productView.length === 0,
              tooltip: options.productView.length === 0 ? '该客户没有其他产品，不能使用改版' : ''
            },
          ]" v-if="route.query.type !== '5'" />
          <p v-else>{{ form.productLayout }}</p>
        </ElFormItemBox>
        <ElFormItemBox label="原产品" :prop="`productCode`" isRules v-if="form.productLayout === '改版'">
          <ElSelectBox :data="form" dataName="productCode" :options="options.productView" @changeBtn="(value: string, row: any, item: any) => {
            form.productName = `${item[0].label} 改版一`
          }" />
        </ElFormItemBox>
        <ElFormItemBox label="保留彩稿" :prop="`keepColorDraft`" isRules
          v-if="form.productLayout === '改版' || route.query.type === '5'">
          <ElRadioBox :data="form" dataName="keepColorDraft" :options="[
            {
              label: '不保留',
              value: 0
            },
            {
              label: '仅保留颜色',
              value: 1
            },
            {
              label: '保留颜色+彩稿',
              value: 2
            },
          ]" />
        </ElFormItemBox>
        <ElFormItemBox label="产品名称" :prop="`productName`" isRules>
          <ElInputBox :data="form" dataName="productName" />
        </ElFormItemBox>
        <ElFormItemBox label="产品单价" :prop="`productPrice`" isRules>
          <ElNumberBox :data="form" dataName="productPrice" unit="元" />
        </ElFormItemBox>
        <ElFormItemBox label="产品要求" :prop="`productRequirement`">
          <ElInputBox :data="form" dataName="productRequirement" type="textarea" />
        </ElFormItemBox>
      </div>
    </template>
  </DetailsFormBox>
</template>
<script setup lang="ts">
import { useListComponent } from '../pageTs'; // 引入逻辑
const { dataInfo, options } = useListComponent();
import { reactive, onMounted, getCurrentInstance } from 'vue';
const { proxy } = getCurrentInstance() as any;
const data: any = reactive({
  loading: false,
  loadingMsg: '',
  form: {
    customerCode: "", // 客户编号
    productType: "", // 产品类型
    productLayout: "新版", // 产品版面
    productCode: "", // 产品编号（产品版面为改版时传入选择的产品编号）
    productName: "", // 产品名称
    productPrice: null, // 产品价格
    productSource: 0, // 产品来源：0新系统；1老系统
    productRequirement: "", // 产品要求
    createBy: "", // 创建人code
    keepColorDraft: 0 // 是否保留彩稿信息、颜色信息：0-不保留；1-保留颜色；2-保留颜色+彩稿
  },
});
import { useRoute } from 'vue-router';
// 获取当前路由对象
const route = useRoute();
import { sendMsg } from '@/utils/crossTagMag.ts';
const saveBtn = (form: any) => {
  const isCopy = !!route.query.productCode
  const _form = {
    ...form,
    productCode: form.productLayout === '改版' || isCopy ? form.productCode : '',
    keepColorDraft: form.productLayout === '改版' || isCopy ? form.keepColorDraft : 0,
  }
  proxy.$api.OrderBusniess.AddProduct(_form)
    .then((res: any) => {
      _sun()
    })
    .catch(() => { });
  function _sun() {
    proxy.$message('保存成功')
    sendMsg('save', true);
    window.close();
  }
};
const close = () => {
  window.close();
}
const setCopy = () => {
  const productCode = route.query.productCode
  if (!productCode) return
  data.loading = true
  proxy.$api.OrderBusniess.GetSingleProductByProductCode({
    queryBy: 2,
    queryValue: productCode
  })
    .then((res: any) => {
      const form = data.form
      form.customerCode = res.baseData.customerCode
      form.productType = res.baseData.productType
      form.productLayout = res.baseData.productLayout
      form.productCode = res.baseData.productCode
      form.productPrice = res.baseData.productPrice
      form.productRequirement = res.baseData.productRequirement
      form.productSource = 0
      form.keepColorDraft = 1
      form.productName = `${res.baseData.productName} 复制`
      data.loading = false
    })
    .catch(() => { });
}
import { getProductType } from '@/utils/optionsArr.ts';
onMounted(() => {
  setCopy()
  getProductType(options, 'productType', (row: any) => {
    data.form.productType = row.objectCode
  }) //获取所有员工
});
</script>
